﻿package mortal.game.scene3D.swf3D
{
    import com.gengine.resource.*;
    import com.gengine.resource.info.*;
    import flash.display.*;
    import flash.geom.*;
    import flash.utils.*;

    public class Swf3DInfo extends Object
    {
        public var bitmapData:BitmapData;
        private var _frameRate:int = 30;
        public var skipFrame:int = 1;
        private var _totalFrame:int = 10;
        public var width:int = 100;
        public var height:int = 100;
        public var widthNum:int = 1;
        public var heightNum:int = 1;
        public var imgNum:int = 1;
        public var isloaded:Boolean = false;
        private var waiterList:Dictionary;
        private static const cachMap:Dictionary = new Dictionary(false);

        public function Swf3DInfo()
        {
            this.waiterList = new Dictionary(false);
            return;
        }// end function

        public function addWaiter(param1:Swf3DPlayer) : void
        {
            this.waiterList[param1] = param1;
            return;
        }// end function

        public function removeWaiter(param1:Swf3DPlayer) : void
        {
            delete this.waiterList[param1];
            return;
        }// end function

        private function loadSwf(param1:String) : void
        {
            var _loc_2:* = param1.replace(/\.swf""\.swf/i, "");
            var _loc_3:* = _loc_2.split("_");
            var _loc_4:* = _loc_3.length;
            if (_loc_4 >= 2)
            {
                this.skipFrame = Number(_loc_3[(_loc_4 - 1)]);
                if (isNaN(this.skipFrame))
                {
                    this.skipFrame = 1;
                }
            }
            var _loc_5:* = ResourceManager.getInfoByName(param1);
            if (!_loc_5)
            {
                _loc_5 = new SWFInfo(null);
                _loc_5.name = param1;
                _loc_5.type = ".swf";
                var _loc_6:* = "/assets/uifla/swf3d/" + param1 + "?time=" + int(Math.random() * 10000);
                _loc_5.loaclPath = "/assets/uifla/swf3d/" + param1 + "?time=" + int(Math.random() * 10000);
                _loc_5.path = _loc_6;
                ResourceManager.addResource(_loc_5);
            }
            LoaderManager.instance.load(param1, this.onLoaded);
            return;
        }// end function

        private function onLoaded(param1:SWFInfo) : void
        {
            var _loc_9:int = 0;
            var _loc_11:Swf3DPlayer = null;
            var _loc_12:int = 0;
            var _loc_2:* = param1.clip;
            this._frameRate = param1.loaderInfo.frameRate;
            this.width = param1.loaderInfo.width;
            this.height = param1.loaderInfo.height;
            this._totalFrame = _loc_2.totalFrames;
            this.imgNum = this._totalFrame / this.skipFrame;
            var _loc_3:* = 1024 * 1024 / this.width * this.height;
            var _loc_13:int = 30;
            _loc_3 = 30;
            var _loc_13:* = _loc_3;
            this.imgNum = _loc_3;
            this.widthNum = 1024 / this.width;
            this.heightNum = Math.ceil(this.imgNum / this.widthNum);
            var _loc_4:* = 1024 / this.height;
            if (this.heightNum > _loc_4)
            {
                this.heightNum = _loc_4;
                this.imgNum = this.widthNum * this.heightNum;
            }
            this.bitmapData = new BitmapData(1024, this.height * this.heightNum, true, 0);
            var _loc_5:* = new Rectangle(0, 0, this.width, this.height);
            var _loc_6:int = 0;
            var _loc_7:* = new BitmapData(this.width, this.height, true, 0);
            var _loc_8:* = new Point();
            var _loc_10:int = 0;
            while (_loc_10 < this.heightNum)
            {
                
                _loc_12 = 0;
                while (_loc_12 < this.widthNum)
                {
                    
                    _loc_9 = _loc_6 * this.skipFrame + 1;
                    if (_loc_9 > this._totalFrame)
                    {
                        break;
                    }
                    else
                    {
                        _loc_2.gotoAndStop(_loc_9);
                        _loc_6++;
                        _loc_7.fillRect(_loc_5, 0);
                        _loc_7.draw(_loc_2);
                        this.bitmapData.copyPixels(_loc_7, _loc_5, _loc_8, null, null, false);
                        _loc_8.x = _loc_8.x + this.width;
                    }
                    _loc_12++;
                }
                _loc_8.y = _loc_8.y + this.height;
                _loc_8.x = 0;
                _loc_10++;
            }
            _loc_7.dispose();
            this.isloaded = true;
            for each (_loc_11 in this.waiterList)
            {
                
                _loc_11.onLoaded(this);
                this.removeWaiter(_loc_11);
            }
            return;
        }// end function

        public static function getSwfInfo(param1:String) : Swf3DInfo
        {
            var _loc_2:* = cachMap[param1];
            if (_loc_2 == null)
            {
                var _loc_3:* = new Swf3DInfo;
                cachMap[param1] = new Swf3DInfo;
                _loc_2 = _loc_3;
                _loc_2.loadSwf(param1);
            }
            return _loc_2;
        }// end function

    }
}
